﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Agents.Admin
{
    //this page allows administrater to add a new agent with all his details to the database.
    public partial class NewAgent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Bind the regions list
                Queries queries = new Queries();
                cmbArea.DataSource = queries.GetAllRegions();
                cmbArea.DataValueField = "Id";
                cmbArea.DataTextField = "Name";
                cmbArea.DataBind();
            }
        }

        //function activated when the ok button is pressed
        protected void okBtn_Click(object sender, EventArgs e)
        {
            //creating a query object for activating the database.
            Queries q = new Queries();

            //making sure all text boxes have been filled
            if (nameTxtbox.Text == "" || phoneTxtbox.Text == "" || emailTxtbox.Text == ""
                || cmbArea.SelectedItem == null || idTxtbox.Text == "" || usernameTxtbox.Text == ""
                || passwordTxtbox.Text == "")
            {
                //if something is missing- print apropriot message to screan.
                empty.Text = "קיימים שדות ריקים. ";
            }
            // Make sure the passwords match
            else if (passwordTxtbox.Text != passwordVerificationTxtbox.Text)
            {
                empty.Text = "הסיסמאות אינן תואמות";
            }
            else
            {   //making sure the new client does not exist in our data base.
                if (q.AgentExists(nameTxtbox.Text) == false)
                {
                    // Disable the buttons so the user can't interfere
                    backBtn.Enabled = false;
                    okBtn.Enabled = false;

                    //adding the new client into our database
                    q.AddAgent(nameTxtbox.Text, phoneTxtbox.Text, idTxtbox.Text,
                    emailTxtbox.Text, Int32.Parse(cmbArea.SelectedValue),
                    usernameTxtbox.Text, passwordTxtbox.Text);

                    // Announce success and wait for 2 seconds before redirecting
                    empty.Text = "הסוכן נוסף בהצלחה";
                    redirectionTimer.Enabled = true;
                }
                //if the client already exists- print apropriate message to screan
                else
                {
                    empty.Text = "שם הסוכן כבר נמצא במערכת";
                }
            }
        }

        //function redirects the administrater back to his homepage.
        protected void backBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/AdminMenu.aspx");
        }

        /// <summary>
        /// Redirect back to the admin menu after a few seconds
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void redirectionTimer_Tick(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/AdminMenu.aspx");
        }
    }
}